package com.yyk.yiliao.ui.home.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;

import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.ui.gongyong.activity.DetailList_Activity;
import com.yyk.yiliao.adapter.anzhengzhaoyao.MyGridViewAdapter;
import com.yyk.yiliao.adapter.anzhengzhaoyao.MyListViewAdapter;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ebs.AzzyEvent;
import com.yyk.yiliao.util.ebs.TwoStringEvent;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.StoreCustomclass_Info;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 按症找药
 */
public class AnZhengZhaoYao2_Activity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.gridView)
    GridView gridView;

    private String cid;
    private String postion;
    private MyListViewAdapter adapter;




    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_an_zheng_zhao_yao_;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("按症找药");
    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        setView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessage(AzzyEvent messageEvent) {
        cid = messageEvent.getCid();
        postion = messageEvent.getPostion();
        initGridView(cid);

    }

    private void setView() {
        initListView();
    }

    private void initGridView(String cid) {
        Map<String, String> map = new TreeMap<>();

        map.put("cid", cid + "");
        map.put("type", 1 + "");

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postStoreCustomclass(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<StoreCustomclass_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();


                    }

                    @Override
                    public void onNext(StoreCustomclass_Info info) {
                        final List<StoreCustomclass_Info.DataBean> data = info.getData();
                        gridView.setAdapter(new MyGridViewAdapter(AnZhengZhaoYao2_Activity.this, data));
                        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(AnZhengZhaoYao2_Activity.this, DetailList_Activity.class);
                                startActivity(intent);
//                                ToastUtil.showShort(AnZhengZhaoYao2_Activity.this, "sss" + data.get(position).getId());
                                EventBus.getDefault().postSticky(new TwoStringEvent(data.get(position).getId() + "", data.get(position).getName() + ""));
                            }
                        });

                    }


                });
        try {
            //因为网络请求在子线程，所以不能让宿主线程过早的结束。
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }

    private void initListView() {
        Map<String, String> map = new TreeMap<>();
        map.put("type", 1 + "");

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postStoreCustomclass(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<StoreCustomclass_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();


                    }

                    @Override
                    public void onNext(StoreCustomclass_Info info) {
                        final List<StoreCustomclass_Info.DataBean> data = info.getData();
                        adapter = new MyListViewAdapter(AnZhengZhaoYao2_Activity.this, data);
                        adapter.setSelected(Integer.parseInt(postion));
                        adapter.notifyDataSetChanged();
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                String cid = String.valueOf(data.get(position).getId());
                                initGridView(cid);
                                //改变被点击的item的背景，字体颜色
                                adapter.setSelected(position);
                                //加载显示子类数据
                                adapter.notifyDataSetChanged();

                            }
                        });
                        listView.setAdapter(adapter);

                    }


                });
        try {
            //因为网络请求在子线程，所以不能让宿主线程过早的结束。
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }


    }
}




